package com.kgc.invitation.invitationmanagerservice.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.invitation.bean.Invitation;
import com.kgc.invitation.bean.InvitationExample;
import com.kgc.invitation.bean.ReplyDetailExample;
import com.kgc.invitation.invitationmanagerservice.mapper.InvitationMapper;
import com.kgc.invitation.invitationmanagerservice.mapper.ReplyDetailMapper;
import com.kgc.invitation.service.InviService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InviServiceImpl implements InviService {
    @Resource
    InvitationMapper invitationMapper;
    @Resource
    ReplyDetailMapper replyDetailMapper;
    @Override
    public String getAllByTitle(String title,Integer pageNum) {
        PageHelper.startPage(pageNum,1);
        PageHelper.orderBy("createdate desc");
        List<Invitation> invitations =null;
        if(!"".equals(title)){
        InvitationExample example=new InvitationExample();
        example.createCriteria().andTitleLike("%"+title+"%");
             invitations = invitationMapper.selectByExample(example);
        }else{
            invitations = invitationMapper.selectByExample(null);
        }
        PageInfo<Invitation> pageInfo=new PageInfo<>(invitations);
        String s= JSONObject.toJSONString(pageInfo, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
        return s;
    }

    @Override
    public Integer deleInvi(Integer id) {
        int i = invitationMapper.deleteByPrimaryKey(id);
        if(i>0){
            ReplyDetailExample example=new ReplyDetailExample();
            example.createCriteria().andInvidEqualTo(id);
            replyDetailMapper.deleteByExample(example);
        }
        return i;
    }
}
